-- 运营活动: 每日充值


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动  
    act_id = 1, -- 活动id (每日充值)
    act_name = '每日充值', -- 活动名称
    act_desc = '充值对应金额，可以领取对应奖励，充值金额不累计', -- 活动描述
    beg_time = 1488956400,  -- 活动开始时间(unix timestamp)
    end_time = 1489042800,  -- 活动结束时间(unix timestamp)
    config = { -- 配置数据
        Gold = {100,200,300},     --充值元宝
        Days = 3,               --活动天数  --感觉这字段并没有什么卵用
        Rewards = 
        {

            {       --第一档奖励
                {type = 0, id = 271, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},
                {type = 0, id = 271, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},  
                {type = 0, id = 271, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},
                {type = 0, id = 271, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},  
            },
            {       --第二档奖励
                {type = 0, id = 272, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},
                {type = 0, id = 272, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},  
                {type = 0, id = 272, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},
                {type = 0, id = 272, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},  
            },
            {       --第三档奖励
                {type = 0, id = 273, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},
                {type = 0, id = 273, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},  
                {type = 0, id = 273, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},
                {type = 0, id = 273, count = 1, bind = 1 , strong = 0 ,quality = 0 ,},  
            },
        },

    },
}
]]

--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_1_check_cfg(cfg)
	if cfg == nil then return 20 end
	
	local config = cfg.config
	if config == nil then return 21 end
	
	if config.Rewards == nil then return 23 end
	if type(config.Rewards) ~= "table" then return 24 end
	for idx = 1, #config.Rewards do
		local dayRewards = config.Rewards[idx]
		if dayRewards == nil then return 25 end
		if type(dayRewards) ~= "table" then return 26 end
		if not oper_act_check_one_award_cfg(dayRewards) then
			return 22
		end
	end

	return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_1_setup(cfg)	
	return 0
end

-- 卸载活动
function oper_act_1_unsetup(cfg)	
end

-- 活动开始
function oper_act_1_start(cfg)
	local ssVar = System.getStaticVar()
	ssVar.oper_act_1_start = 1
	ssVar.oper_act_1_cmd = cfg.cmd_id
	ssVar.oper_act_1_act = cfg.act_id

	local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_1, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return
        end
    end

	gRankMgr_Clear(RANK_DEFINE_OPERATION_1)
end

-- 活动结束
function oper_act_1_end(cfg)
	local ssVar = System.getStaticVar()
	ssVar.oper_act_1_start = nil
	ssVar.oper_act_1_cmd = nil
	ssVar.oper_act_1_act = nil

	--活动结束发邮件
	
	if not cfg then
		return 
	end
	config = cfg.config
	if not config then
		return
	end
	local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_1].strName)
	local rankCount = Ranking.getRankItemCount(rankPtr)
	for idx = 1, rankCount do
		local item = Ranking.getItemFromIndex(rankPtr, idx-1)
		if item then
			local actorid = tonumber(Ranking.getId(item))
			local data = Ranking.getSub(item, 1)
			local dataTable
			if not data or data == "" then
				dataTable = {}
			else
				dataTable = BaseFuc_unserialize(data)
			end
			if dataTable then
				for idx = 1, #config.Gold do
					if dataTable[idx] and dataTable[idx] == 1 then
						local rewards = config.Rewards[idx]
						local title = Lang.ScriptTips.Operation002
						local content = Lang.ScriptTips.Operation003
						Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId001, Lang.LogTips.Operation001, dataTable.vocation, dataTable.sex)
						break
					end
				end
			end
		end
	end
end

-- 打包数据
function oper_act_1_packet_data(sysarg, cfg)
	
	--DataPack.writeByte(netPack, 1)		-- 活动特有参数
	--发送每日充值数据

	local ssVar = System.getStaticVar()
	if not ssVar.oper_act_1_start then
		return
	end
	
	local config = cfg.config
	if not config then 
		return 
	end

	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_1, 1)
	local myData = BaseFuc_unserialize(data)
	

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end


	local time = System.getSystemTime()
	if myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
		SendOperationLastTimeMail(sysarg, cfg, myData)

		myData = {}
		local newData = BaseFuc_serialize(myData)
		gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_1, 1, newData)
	end


	local pack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
	if (pack == nil) then
		return
	end
	local len = #config.Gold
	for idx = 1, len do
		local state = myData[idx] or 0
		if state < 2 or (state == 2 and idx == len) then
			DataPack.writeByte(pack, idx)
			DataPack.writeByte(pack, state)
			break
		end
	end
	DataPack.flush(pack)
end

-- 操作，返回操作结果，0为成功
function oper_act_1_do(sysarg, cfg, clientPkt)
	if cfg == nil then return end
	
	if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
		return
	end
	
	local config = cfg.config
	if config == nil then -- 参数错误
		print("error, oper_act_1_do cfg.config confg error")
		return
	end

	local Index = DataPack.readByte(clientPkt)
	local ssVar = System.getStaticVar()
	if not ssVar.oper_act_1_start then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Operation004, ttFlyTip)
		return
	end
	
	if Index > #config.Gold then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Operation005, ttFlyTip)
		return
	end
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_1, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Operation006, ttFlyTip)
		return
	end
	if not myData[Index] or myData[Index] ~= 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Operation006, ttFlyTip)
		return
	end


	local rewards = config.Rewards[Index]

	myData[Index] = 2
	
	if IsBagGridEnough(sysarg, rewards) then
		GiveCommonAward(sysarg, rewards, LogIds.OperationLogId001, Lang.LogTips.Operation001)
	else
		local actorId = Actor.getActorId(sysarg)
		local title = Lang.ScriptTips.Operation002
		local content = Lang.ScriptTips.Operation003

		local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

		Awards.GiveToMailById(actorId, rewards, title, content, LogIds.OperationLogId001, Lang.LogTips.Operation001, vocation, sex)
	end

	local time = System.getSystemTime()
	if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end

    myData.mySelfTime = time

	local newData = BaseFuc_serialize(myData)
	gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_1, 1, newData)
		
	
	local netPack = oper_act_alloc_do_result_packet(sysarg, cfg.cmd_id, cfg.act_id, 0)
	if not netPack then return end
	local len = #config.Gold
	for idx = 1, len do
		local state = myData[idx] or 0
		if state < 2 or (state == 2 and idx == len) then
			DataPack.writeByte(netPack, idx)
			DataPack.writeByte(netPack, state)
			break
		end
	end
--	DataPack.writeByte(netPack, 1) -- 活动特有参数
	DataPack.flush(netPack)
end


function SendOperationLastTimeMail(sysarg, cfg, dataTable)
	if not cfg then
		return 
	end
	local config = cfg.config
	if not config then
		return
	end
	local actorid = Actor.getActorId(sysarg)
	if dataTable then
		for idx = 1, #config.Gold do
			if dataTable[idx] and dataTable[idx] == 1 then
				local rewards = config.Rewards[idx]
				local title = Lang.ScriptTips.Operation002
				local content = Lang.ScriptTips.Operation003
				Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId001, Lang.LogTips.Operation001, dataTable.vocation, dataTable.sex)
				break
			end
		end
	end
end

function oper_act_1_new_day_arrive(act_id)	
	local ssVar = System.getStaticVar()
	if not ssVar.oper_act_1_start then
		return
	end
	if not ssVar.oper_act_1_cmd then
		return
	end
	if not ssVar.oper_act_1_act then
		return
	end
	local cfg = get_oper_act_cfg(ssVar.oper_act_1_cmd, ssVar.oper_act_1_act)

	--活动结束发邮件
	
	if not cfg then
		return 
	end
	local config = cfg.config
	if not config then
		return
	end
	local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_1].strName)
	local rankCount = Ranking.getRankItemCount(rankPtr)
	for idx = 1, rankCount do
		local item = Ranking.getItemFromIndex(rankPtr, idx-1)
		if item then
			local actorid = tonumber(Ranking.getId(item))
			local data = Ranking.getSub(item, 1)
			local dataTable
			if not data or data == "" then
				dataTable = {}
			else
				dataTable = BaseFuc_unserialize(data)
			end
			if dataTable then
				for idx = 1, #config.Gold do
					if dataTable[idx] and dataTable[idx] == 1 then
						local rewards = config.Rewards[idx]
						local title = Lang.ScriptTips.Operation002
						local content = Lang.ScriptTips.Operation003
						Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId001, Lang.LogTips.Operation001, dataTable.vocation, dataTable.sex)
						break
					end
				end
			end
		end
	end
	
	gRankMgr_Clear(RANK_DEFINE_OPERATION_1)

	local playerList = LuaHelp.getAllActorList()  --获取在线玩家对象列表
	if not playerList then
		return
	end
	for _, sysarg  in ipairs(playerList) do
		oper_act_1_packet_data(sysarg, cfg)
	end
end

--运营活动1-每日充值
function oper_act_1_withdrawyuanbao(sysarg, Gold)
	
	local ssVar = System.getStaticVar()
	if not ssVar.oper_act_1_start then
		return
	end
	if not ssVar.oper_act_1_cmd then
		return
	end
	if not ssVar.oper_act_1_act then
		return
	end
	local cfg = get_oper_act_cfg(ssVar.oper_act_1_cmd, ssVar.oper_act_1_act)
	if not cfg then
		return
	end
	local config = cfg.config
	if not config then
		return
	end
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_1, 1)

	local myData = BaseFuc_unserialize(data)

	if not myData then
		myData = {}
	end

	local time = System.getSystemTime()
	if myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
		SendOperationLastTimeMail(sysarg, cfg, myData)
		myData = {}
	end

	for idx = 1, #config.Gold do
		if myData[idx] and myData[idx] > 1 then
		elseif myData[idx] and myData[idx] == 1 then
			break
		else
			if config.Gold[idx] <= Gold then
				myData[idx] = 1

				if not myData.cmd_id then
	                myData.cmd_id = cfg.cmd_id
	                myData.act_id = cfg.act_id
	                myData.time = time
	            end

	            if not myData.sex then
			        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
			        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
			    end

	            myData.mySelfTime = time

				local newData = BaseFuc_serialize(myData)
				gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_1, 1, newData)
				oper_act_1_packet_data(sysarg, cfg)
			end
			break
		end
	end
end

